package com.petalsgroup.petals.util;

import java.util.ArrayList;
import java.util.List;

public class LimitUtil {
    // 总记录数
    private Long total;
    // 当前页码
    private Integer currentPage;
    // 显示的页码
    private List<Integer> pages;
    // 页数
    private Integer maxPage;
    // 每页的记录数
    private Integer num;
    // 最小页数
    private Integer minPage = 1;
    // 查询开始页数
    private Integer select = 0;

    public LimitUtil() {
    }

    public LimitUtil(long total, int num) {
        init(total, num, 1);
    }

    public LimitUtil(long total, int num, int currentPage) {
        init(total, num, currentPage);
    }

    public void init(long total, int num, int currentPage) {
        this.total = total;
        this.num = num;
        this.currentPage = currentPage;
        this.maxPage = (int)(this.total / this.num) + ((this.total % this.num > 0) ? 1 : 0);
        this.select = (this.currentPage - 1) * this.num;
        getPages(currentPage);
    }

    private void getPages(int currentPage) {
        this.pages = new ArrayList<>(3);
        for (int i = 0; i < 3; i++) {
            this.pages.add(currentPage);
            if (currentPage == minPage) {
                break;
            }
            currentPage--;
        }
        List<Integer> temp = new ArrayList<>(5);
        for (int i = this.pages.size(); i != 0; i--) {
            temp.add(this.pages.get(i - 1));
        }
        this.pages = temp;
        currentPage = this.currentPage.intValue();
        int len = this.pages.size();

        for (int i = 0; i < (5 - len); i++) {
            if (currentPage == this.maxPage) {
                break;
            }
            currentPage++;
            this.pages.add(currentPage);
        }

    }

    public void flash(long total, int num) {
        init(total, num, 1);
    }

    public void flash(long total) {
        init(total, this.num, 1);
    }

    public void jump(int currentPage) {
        if (currentPage >= minPage && currentPage <= maxPage) {
            this.currentPage = currentPage;
            getPages(this.currentPage);
            this.select = (this.currentPage - 1) * this.num;
        }
    }

    public boolean hasNext() {
        if (this.currentPage == this.maxPage) {
            return false;
        }
        return true;
    }

    public boolean hasPre() {
        if (this.currentPage == this.minPage) {
            return false;
        }
        return true;
    }

    public void next() {
        if (hasNext()) {
            this.currentPage++;
            getPages(this.currentPage);
            this.select += this.num;
        }
    }

    public void pre() {
        if (hasPre()) {
            this.currentPage--;
            getPages(this.currentPage);
            this.select -= this.num;
        }
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
        getPages(this.currentPage);
    }

    public List<Integer> getPages() {
        return pages;
    }

    public void setPages(List<Integer> pages) {
        this.pages = pages;
    }

    public Integer getMaxPage() {
        return maxPage;
    }

    public void setMaxPage(Integer pageNum) {
        this.maxPage = pageNum;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public Integer getSelect() {
        this.select = (this.currentPage - 1) * this.num;
        return select;
    }

    public void setSelect(Integer select) {
        this.select = select;
    }

}
